package org.mclll.xwangplugin.town.command.executor;

import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownApplyDO;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownApplyManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.ApplyStatus;
import org.mclll.xwangplugin.town.enums.ApplyType;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;

import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/24 00:49
 */
public class PlayApplyJoinExecutor {
    public static void applyJoin(Player player, String[] args) {
        if (args.length != 2) {
            player.sendMessage(PLAYER_APPLY_JOIN_COMMAND_FORMAT);
            return;
        }
        String townName = args[1];
        TownDO applyTown = null;
        TownDO checkParam = new TownDO();
        checkParam.setName(townName);
        List<TownDO> checkTownList = TownManage.selectList(checkParam);
        if (checkTownList.isEmpty()) {
            player.sendMessage(TOWN_NOT_FOUND);
            return;
        }
        String playerUuid = player.getUniqueId().toString();
        TownPlayerDO townPlayer = TownPlayerManage.selectByUuid(playerUuid);
        if (townPlayer.getIdentity() != PlayerTownIdentity.GENSHIN) {
            player.sendMessage(String.format(PLAYER_ALREADY_HAVE_TOWN, "你！你！"));
            return;
        }
        applyTown = checkTownList.get(0);
        TownApplyDO checkApply = new TownApplyDO();
        checkApply.setPlayerUuid(playerUuid);
        checkApply.setTownUuid(applyTown.getUuid());
        checkApply.setType(ApplyType.PLAYER_APPLICATION);
        checkApply.setStatus(ApplyStatus.APPLYING);
        List<TownApplyDO> checkApplyList = TownApplyManage.selectList(checkApply);
        if (!checkApplyList.isEmpty()) {
            player.sendMessage(String.format(PLAYER_ALREADY_APPLY_JOIN, applyTown.getName()));
            return;
        }
        TownApplyDO townApply = new TownApplyDO();
        townApply.setPlayerUuid(playerUuid);
        townApply.setTownUuid(applyTown.getUuid());
        townApply.setTownName(applyTown.getName());
        townApply.setTownParty(applyTown.getTownParty());
        townApply.setType(ApplyType.PLAYER_APPLICATION);
        townApply.setStatus(ApplyStatus.APPLYING);
        TownApplyManage.insert(townApply);
        player.sendMessage(String.format(PLAYER_APPLY_JOIN_SUCCESS, applyTown.getName()));
    }
    public static List<String> applyJoinTabComplete(String[] args) {
        if (args.length == 2) {
            return TownManage.selectList(null).stream()
                    .map(TownDO::getName)
                    .filter(townName -> townName.startsWith(args[1]))
                    .collect(java.util.stream.Collectors.toList());
        }
        return null;
    }
}
